// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TransactionTypeResolver.cs" company="">
//   
// </copyright>
// <summary>
//   The transaction type resolver.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace XRay.Library
{
    using System;

    using AutoMapper;

    using Microsoft.WindowsAzure.Storage.Table;

    using XRay.Library.Contracts;

    /// <summary>
    /// The transaction type resolver.
    /// </summary>
    public class TransactionTypeResolver : ValueResolver<TableEntity, TransactionTypeEnum>
    {
        #region Methods

        /// <summary>
        /// The resolve core.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <returns>
        /// The <see cref="TransactionTypeEnum"/>.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        protected override TransactionTypeEnum ResolveCore(TableEntity source)
        {
            var split = source.RowKey.Split(';');

            if (split.Length != 2)
            {
                throw new ArgumentOutOfRangeException("entity.rowkey");
            }

            return (TransactionTypeEnum)Enum.Parse(typeof(TransactionTypeEnum), split[1]);
        }

        #endregion
    }
}